package com.action;

import org.apache.struts2.ServletActionContext;

import com.bean.Admin;
import com.bean.Student;
import com.bean.Teacher;
import com.dao.AdminDao;
import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.opensymphony.xwork2.ActionSupport;
import com.util.HttpUtil;

public class UpdatePassAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6399370225604108819L;
	
	private String currentP;
	
	private String newP;
	
	public String updateP(){
		String type = (String) ServletActionContext.getRequest().getSession().getAttribute("type");
		//System.out.println(type);
		if(type.equals("1")){
			Student stu = (Student) ServletActionContext.getRequest().getSession().getAttribute("stu");
			//System.out.println("密码");
			//System.out.println(stu.getStuPassword());
			StudentDao sd = new StudentDao();
			if(stu.getStuPassword().equals(currentP.trim())){
				if(sd.updateP(stu, newP)==1){
					stu.setStuPassword(newP);
					HttpUtil.setSession("stu", stu);
					//System.out.println(stu.getStuPassword());
				}	
			}
		}else if(type.equals("2")){
			Teacher tea = (Teacher) ServletActionContext.getRequest().getSession().getAttribute("tea");
			TeacherDao td = new TeacherDao();
			if(tea.getTeaPassword().equals(currentP.trim())){
				if(td.updateP(tea, newP)==1){
					tea.setTeaPassword(newP);
					HttpUtil.setSession("tea", tea);
					//System.out.println(tea.getTeaPassword());
				}	
			}
		}
		return SUCCESS;
		
	}
	
	public String updatePassword(){
		Admin adm = (Admin) ServletActionContext.getRequest().getSession().getAttribute("adm");
		AdminDao ad = new AdminDao();
		if(adm.getAdmPassWord().equals(currentP.trim())){
			if(ad.updateP(adm, newP)==1){
				adm.setAdmPassWord(newP);
				HttpUtil.setSession("adm", adm);
				return SUCCESS;
			}
			
		}else {
			this.addFieldError("password", "当前密码输入错误请重新输入！");
			return INPUT;
			
		}
		
		return INPUT;
	}

	public String getCurrentP() {
		return currentP;
	}

	public void setCurrentP(String currentP) {
		this.currentP = currentP;
	}

	public String getNewP() {
		return newP;
	}

	public void setNewP(String newP) {
		this.newP = newP;
	}
	
}
